package com.mkyong;

import java.util.Date;

import org.hibernate.Session;

import com.mkyong.stock.Stock;
import com.mkyong.stock.StockDailyRecord;
import com.mkyong.util.HibernateUtil;

public class App {
	public static void main(String[] args) {
		//insert();
		delete();
	}
	
	public static void insert(){
		System.out.println("Hibernate one to many (Annotation)");
		Session session = HibernateUtil.getSessionFactory().openSession();

		session.beginTransaction();
		//
		
		Stock stock = new Stock();
        stock.setStockCode("7052");
        stock.setStockName("PADINI");
        //session.save(stock);
        
        StockDailyRecord stockDailyRecords = new StockDailyRecord();
        stockDailyRecords.setPriceOpen(new Float("91.1115"));
        stockDailyRecords.setPriceClose(new Float("626.6555"));
        stockDailyRecords.setPriceChange(new Float("18.123"));
        stockDailyRecords.setVolume(3321123L);
        stockDailyRecords.setDate(new Date());
        
        StockDailyRecord stockDailyRecords2 = new StockDailyRecord();
        stockDailyRecords2.setPriceOpen(new Float("91.2115"));
        stockDailyRecords2.setPriceClose(new Float("661.6555"));
        stockDailyRecords2.setPriceChange(new Float("54.123"));
        stockDailyRecords2.setVolume(6666666L);
        stockDailyRecords2.setDate(new Date(2015, 1, 1));
        
        
        stockDailyRecords.setStock(stock);   
        stockDailyRecords2.setStock(stock);
        stock.getStockDailyRecords().add(stockDailyRecords);
        stock.getStockDailyRecords().add(stockDailyRecords2);
        
        
        session.save(stock);
		
		session.getTransaction().commit();
		System.out.println("Done");
	}
	
	public static void delete(){
		System.out.println("Hibernate one to many (Annotation)");
		Session session = HibernateUtil.getSessionFactory().openSession();
		
		session.beginTransaction();
		
		session.createQuery("delete from Stock where 1=1").executeUpdate();
		
		session.getTransaction().commit();
		System.out.println("Done");
		
		
	}
}
